package com.baozhun.mall.common.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Paint.FontMetrics
import android.graphics.RectF
import com.baozhun.mall.common.R
import com.youth.banner.indicator.BaseIndicator
import com.youth.banner.util.BannerUtils


/**
 * @Title：数字指示器
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/6/14
 * @Version：1.0.0
 */
class NumIndicator(context: Context?) : BaseIndicator(context) {

    var viewWidth = 0
    var viewHeight = 0
    var radius = 0

    init {
        mPaint.textSize = BannerUtils.dp2px(12f).toFloat()
        mPaint.textAlign = Paint.Align.CENTER
        viewWidth = BannerUtils.dp2px(32F)
        viewHeight = BannerUtils.dp2px(18F)
        radius = BannerUtils.dp2px(10F)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val count = config.indicatorSize
        if (count <= 1) {
            return
        }
        setMeasuredDimension(viewWidth, viewHeight)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val count = config.indicatorSize
        if (count <= 1) {
            return
        }
        val rectF = RectF(0F, 0F, viewWidth.toFloat(), viewHeight.toFloat())
        mPaint.color = context.getColor(com.baozhun.mall.res.R.color.black_10)
        canvas.drawRoundRect(rectF, radius.toFloat(), radius.toFloat(), mPaint)
        val text = "${config.currentPosition + 1}/${count}"
        mPaint.color = Color.WHITE

        // 计算文字高度
        val fontMetrics: FontMetrics = mPaint.fontMetrics
        val fontHeight: Float = fontMetrics.bottom - fontMetrics.top
        // 计算文字baseline
        val textBaseY = height - (height - fontHeight) / 2 - fontMetrics.bottom
        canvas.drawText(text, (viewWidth / 2).toFloat(), textBaseY, mPaint)
    }
//
//    val binding by lazy { DataBindingUtil.bind<ItemIndicatorTextBinding>(View.inflate(context, R.layout.item_indicator_text, null)) }
//    override fun getIndicatorView(): View {
//        return binding?.root!!
//    }
//
//    override fun onPageChanged(count: Int, currentPosition: Int) {
//        super.onPageChanged(count, currentPosition)
//        binding?.tvIndicator?.text = "$currentPosition / $count"
//    }


}